package org.tio.sitexxx.im.server;

import java.util.concurrent.atomic.AtomicLong;

import org.lionsoul.ip2region.DataBlock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.ChannelContext;
import org.tio.core.intf.Packet;
import org.tio.server.intf.ServerAioListener;
import org.tio.sitexxx.im.common.Command;
import org.tio.sitexxx.im.common.CommandStat;
import org.tio.sitexxx.im.common.ImPacket;
import org.tio.sitexxx.im.common.ImSessionContext;
import org.tio.sitexxx.service.ip2region.Ip2Region;
import org.tio.sitexxx.service.vo.Const;
import org.tio.utils.SystemTimer;

/**
 *
 * @author tanyaowu
 *
 */
public class TioSiteImServerAioListener implements ServerAioListener {
	private static Logger	log		= LoggerFactory.getLogger(TioSiteImServerAioListener.class);

	//	static Map<String, AtomicLong> ipmap = new java.util.concurrent.ConcurrentHashMap<>();
	static AtomicLong accessCount = new AtomicLong();

	/**
	 * @param args
	 *
	 * @author tanyaowu
	 * 2016年12月16日 下午5:52:06
	 *
	 */
	public static void main(String[] args) {
	}

	public static final TioSiteImServerAioListener me = new TioSiteImServerAioListener();

	/**
	 *
	 *
	 * @author tanyaowu
	 * 2016年12月16日 下午5:52:06
	 *
	 */
	private TioSiteImServerAioListener() {
	}

	/**
	 * @see org.tio.core.intf.AioListener#onAfterClose(org.tio.core.ChannelContext, java.lang.Throwable, java.lang.String)
	 *
	 * @param channelContext
	 * @param throwable
	 * @param remark
	 * @author tanyaowu
	 * 2016年2月1日 上午11:03:11
	 *
	 */
	//	@Override
	//	public void onAfterClose(ChannelContext channelContext, Throwable throwable, String remark, boolean isRemove) {
	//	}

	/**
	 * @see org.tio.server.intf.ServerAioListener#onAfterAccepted(java.nio.channels.AsynchronousSocketChannel, org.tio.server.TioServer)
	 *
	 * @param asynchronousSocketChannel
	 * @param tioServer
	 * @return
	 * @author tanyaowu
	 * 2016年12月20日 上午11:03:45
	 *
	 */
	//	@Override
	//	public boolean onAfterAccepted(AsynchronousSocketChannel asynchronousSocketChannel, TioServer tioServer)
	//	{
	//		return true;
	//	}

	@Override
	public void onAfterConnected(ChannelContext channelContext, boolean isConnected, boolean isReconnect) {
		ImSessionContext imSessionContext = new ImSessionContext();
		channelContext.setAttribute(Const.IM_SESSION_KEY, imSessionContext);

		//		GroupContext groupContext = channelContext.groupContext;

		if (isConnected) {
			String ip = channelContext.getClientNode().getIp();

			DataBlock dataBlock = Ip2Region.getDataBlock(ip);
			imSessionContext.setDataBlock(dataBlock);
		}

		return;
	}

	/**
	 * 
	 * @param channelContext
	 * @param packet
	 * @param packetSize
	 * @author: tanyaowu
	 */
	@Override
	public void onAfterDecoded(ChannelContext channelContext, Packet packet, int packetSize) {
		if (packet instanceof ImPacket) {
			ImPacket imPacket = (ImPacket) packet;
			CommandStat.getCount(imPacket.getCommand()).received.incrementAndGet();
		}
	}

	/**
	 * 
	 * @param channelContext
	 * @param packet
	 * @param isSentSuccess
	 * @author: tanyaowu
	 */
	@Override
	public void onAfterSent(ChannelContext channelContext, Packet packet, boolean isSentSuccess) {
		if (packet instanceof ImPacket) {
			ImPacket imPacket = (ImPacket) packet;
			if (isSentSuccess) {
				CommandStat.getCount(imPacket.getCommand()).sent.incrementAndGet();
			}
		}
	}

	@Override
	public void onBeforeClose(ChannelContext channelContext, Throwable throwable, String remark, boolean isRemove) {
		long groupChatCount = CommandStat.getCount(Command.GROUP_CHAT_REQ).received.get(); //这个人发了多少条群聊消息
		long p2pChatCount = CommandStat.getCount(Command.P2P_CHAT_REQ).received.get(); //这个人发了多少条私聊消息
		log.info("{}, 此人发的群聊数:{}, 此人发的私聊数:{}", channelContext, groupChatCount, p2pChatCount);

		long groupChatCount2 = CommandStat.getCount(Command.GROUP_CHAT_NTY).sent.get(); //服务器发了多少条群聊给这个人
		long p2pChatCount2 = CommandStat.getCount(Command.P2P_CHAT_NTY).sent.get(); //服务器发了多少条私聊给这个人
		log.info("{}, 发往此人的群聊数:{}, 发往此人的私聊数:{}", channelContext, groupChatCount2, p2pChatCount2);

		@SuppressWarnings("unused")
		long iv = SystemTimer.currTime - channelContext.stat.timeCreated; //这个通道的时长，单位：毫秒
	}

	@Override
	public void onAfterHandled(ChannelContext channelContext, Packet packet, long cost) throws Exception {
	}

	@Override
	public void onAfterReceivedBytes(ChannelContext channelContext, int receivedBytes) throws Exception {
		// TODO Auto-generated method stub

	}

}
